package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrdersSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.entity.OrderSetting;
import com.itheima.health.pojo.vo.OrderSettingVo;
import com.itheima.health.service.OrdersSettingService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersSettingServiceImpl implements OrdersSettingService {
    @Autowired
    private OrdersSettingDao ordersSettingDao;

    /**
     * 预约文件上传
     *
     * @param excelFile
     */
    @Override
    public Boolean upload(MultipartFile excelFile) {
        //使用POI读取文件中的信息，将这些信息写入到数据库中
        try (
                InputStream inputStream=excelFile.getInputStream();
                XSSFWorkbook excel=new XSSFWorkbook(inputStream);
        ) {
            //获取excel中的第一个工作表
            XSSFSheet sheet=excel.getSheetAt(0);
            for (int i=1; i <= 30; i++) {
                //获取每一行的对象
                XSSFRow row=sheet.getRow(i);
                if (row == null) {
                    break;
                }
                //每一行的日期对象
                Date date=row.getCell(0).getDateCellValue();
                //每一行的数量对象
                Integer number=(int) row.getCell(1).getNumericCellValue();
                //写入数据库
                ordersSettingDao.insertOrderDateAndNumber(date, number);

                return true;
            }
        } catch (IOException e) {
            throw new BusinessRuntimeException(MessageConst.GET_ORDERSETTING_FAIL);
        }
        return false;
    }

    /**
     * 当月预约信息展示
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<OrderSettingVo> getOrderSettingByMonth(Integer year, Integer month) {
        LocalDate localDate=LocalDate.of(year, month, 1);
        // 构造查询条件
        LocalDate beginDate=localDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate=localDate.with(TemporalAdjusters.lastDayOfMonth());
        List<OrderSettingVo> orderSettingVos=new ArrayList<>();

        // 遍历每一天
        while (!beginDate.isAfter(endDate)) {
            OrderSettingVo orderSettingVo=ordersSettingDao.selectAll(beginDate);
            if (orderSettingVo != null) {
                orderSettingVo.setDate(beginDate.getDayOfMonth());
                orderSettingVos.add(orderSettingVo);
            }
            beginDate=beginDate.plusDays(1);
        }
        return orderSettingVos;
    }


    /**
     * 根据日期修改可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate=orderSetting.getOrderDate();
        LocalDate localDate=orderDate.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        OrderSetting orderSetting1=ordersSettingDao.selectByDate(localDate);
        if (orderSetting1 != null) {
            ordersSettingDao.update(orderSetting);
        } else {
            ordersSettingDao.insertOrder(orderSetting);
        }


    }
}
